package service

import (
	"context"
	"fmt"
	"gitee.com/jinmingzhi/cdce-service/internal/model"
	"gitee.com/jinmingzhi/cdce-service/internal/repository"
	"go.uber.org/zap"
)

type Pay interface {
	FindAll(ctx context.Context, uid uint32) ([]*model.PayList, error)
	FindAllComplate(ctx context.Context, uid uint32) ([]*model.PayList, error)
	ConfirmCost(ctx context.Context, pid, uid uint32) error
}

type payImpl struct {
	PayRepo repository.Pay
}

func NewPay(PayRepo repository.Pay) Pay {
	return &payImpl{
		PayRepo: PayRepo,
	}
}

func (p *payImpl) FindAll(ctx context.Context, uid uint32) ([]*model.PayList, error) {
	data, err := p.PayRepo.FindAllByUid(ctx, uid)
	if err != nil {
		log.Error("payrepo failed", zap.String("uid", fmt.Sprintf("%d", uid)), zap.Error(err))
		return nil, err
	}
	return data, nil
}

func (p *payImpl) FindAllComplate(ctx context.Context, uid uint32) ([]*model.PayList, error) {
	data, err := p.PayRepo.FindAllByUidStatus(ctx, uid, true)
	if err != nil {
		log.Error("payrepo failed", zap.String("uid", fmt.Sprintf("%d", uid)), zap.Error(err))
		return nil, err
	}
	return data, nil
}

func (p *payImpl) ConfirmCost(ctx context.Context, pid, uid uint32) error {
	if err := p.PayRepo.ConfirmCost(ctx, pid, uid); err != nil {
		log.Error("PayRepo.ConfirmCost failed", zap.String("pid", fmt.Sprintf("%d", pid)), zap.Error(err))
		return err
	}
	return nil
}
